home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Saving a C++ object
- Date: Fri, 01 Mar 1996 17:34:58 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4h7cer$8pk@news.halcyon.com>
- References: <3135C74E.446B9B3D@doc.ic.ac.uk>
- NNTP-Posting-Host: blv-pm10-ip9.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Typically, a class has Save() and Load() methods that accept as an
- argument the archive into/from which you want to do the persistence.
- This allows you to "Save" to the clipboard, across a pipe, etc., as
- well as to a file.
-
- Usually, classes write out their member variables item by item instead
- of saving themselves en-masse. There are many reasons: first, the
- sizeof(ovoid) trick stores structure padding as well as data, so it's
- not readily portable or necessarily efficient.
- Second, if you ever do change the format of your class, you're clearly
- broken, whereas in the item-by-item approach, you can read a version
- number as the first item and steer the rest of your load operation
- accordingly.
- Third, you will probably want to call the Save() and Load() methods of
- your base-classes and your contained classes. If you've contained
- objects by reference (pointer), you obviously don't want to save out
- the pointer, rather the pointed-to-object.
- Lastly, loading en-masse into a blob of memory, then type-casting the
- blob is constructing the object properly. Virtual function tables
- will not be created, and virtual functions are the meat of OOP.
- Virtual inheritence will also be suspect.
-
- Loading classes turns out to be tricky: you've saved your drawing,
- let's say, as a Circle, a Square, and a Polygon. When you go to load,
- all you know is that your drawing contains Shape classes, but you
- don't know which ones or in which order.
- Typically you make a special "factory" that knows how to load shapes.
- The Shape deriviatives save out a code that says "I'm a circle," then
- the factory loads this code and walks the list of registered classes
- until it finds the Circle class. It instantiates the Circle, which
- set's it's "I'm a circle" code in the ctor, then the factory calls
- thecircle::Load() to finish loading the members.
-
- Hope this helps.
- --Norm
-
- Ben Jefferys <brj@doc.ic.ac.uk> wrote:
-
- >When I save a C++ object just using:
-
- >----------------------------
- >class ovoid{
- >/* etc. */
- >}
-
- >ovoid square
-
- >file.write((unsigned char *) &square, sizeof(ovoid))
- >----------------------------
-
- >What *exactly* is saved? I'm finding that every time I recompile
- >an application when I've added some new code, trying to load
- >an object saved in this way with an old version of the application
- >crashes it, even if the class of the object in question hasn't
- >changed. ...
-
- >Thanks for your help.
-
- >--
- >Je suis triste et seul ici.
-
-
-